﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using ADEVSO.Util;
using SmartParking.Data.Entities;
using SmartParking.Bussiness;

namespace SmartParking.Web.Manager
{
    public partial class Parkings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                (Master as Logger).Title = "Parkings";
                (Master as Logger).ActiveMenuItem = "Parkings";
                LoadCustomers();
                LoadParkings();
            }
        }

        protected void LoadCustomers()
        {
            ParkingManager.BindCustomers(ref cboxCustomer);
        }

        protected void LoadParkings()
        {
            ParkingManager.BindParkings(ref grdParkings, Convert.ToInt32(cboxCustomer.SelectedValue));
            upParking.Update();
        }

        protected void btnSubmitPark_Click(object sender, EventArgs e)
        {
            Page.Validate("Park");

            if (Page.IsValid)
            {
                //TODO: Add legal id, legal name, address and phone
                ParkingManager.SaveParking(
                    Convert.ToInt32(cboxCustomer.SelectedValue),
                    hfId.Value,
                    txtName.Text,
                    String.Empty,
                    String.Empty,
                    txtBankAccountNumber.Text,
                    String.Empty,
                    String.Empty,
                    Profile.ManagerID,
                    Request.UserHostAddress);

                //mpeAddParking.Hide();
                CloseDialog("pnlParkForm");
                CleanParkForm();
                LoadParkings();
            }
        }

        /// <summary>
        /// Registers client script to close the dialog
        /// </summary>
        /// <param name="dialogId"></param>
        private void CloseDialog(string dialogId)
        {
            string script = string.Format(@"closeDialog('{0}')", dialogId);
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
        }

        /// <summary>
        /// Registers client script to close the dialog
        /// </summary>
        /// <param name="dialogId"></param>
        private void OpenDialog(string dialogId)
        {
            string script = string.Format(@"showDialog('{0}')", dialogId);
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
        }

        protected void grdParkings_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditPark")
            {
                Parking parking = ParkingManager.GetParking(Convert.ToInt32(e.CommandArgument));
                if (parking != null)
                {
                    hfId.Value = e.CommandArgument.ToString();
                    txtName.Text = parking.Name.Trim();
                    //TODO
                    //txtLegalID = parking.LegalID;
                    //txtLegalName = parking.LegalName;
                    txtBankAccountNumber.Text = parking.BankAccountNumber.Trim();
                    //txtAddress.Text = parking.Address;
                    //txtPhone.Text = parking.Phone;
                    btnSubmitPark.Text = "Update";
                    lblParkTitle.Text = "Modify Parking";
                    btnDeletePark.Visible = true;
                    //mpeAddParking.Show();
                    upParkingForm.Update();
                    OpenDialog("pnlParkForm");
                }
            }
            else if (e.CommandName == "DeletePark")
                DeleteParking(Convert.ToInt32(e.CommandArgument));
            else if (e.CommandName == "Zones")
            {
                zonegrid.Visible = zoneheader.Visible = true;
                LoadParkingZones(Convert.ToInt32(e.CommandArgument));
                LoadHours();
                upParkingZoneForm.Update();
            }
        }

        private void DeleteParking(int id)
        {
            ParkingManager.DeleteParking(id, Profile.ManagerID, Request.UserHostAddress);
            LoadParkings();
        }

        protected void btnDeletePark_Click(object sender, EventArgs e)
        {
            DeleteParking(Convert.ToInt32(hfId.Value));
            CleanParkForm();
            upParking.Update();
            CloseDialog("pnlParkForm");
        }

        protected String DisplayHour(Object hour)
        {
            return Convert.ToString(hour).ToHour().ToStringHour();
        }

        protected String DisplayBoolean(Object boolean)
        {
            return Convert.ToBoolean(boolean) ? "Y" : "N";
        }

        protected void btnCancelPark_Click(object sender, EventArgs e)
        {
            CleanParkForm();
            CloseDialog("pnlParkForm");
        }

        private void CleanParkForm()
        {
            btnSubmitPark.Text = "Save";
            lblParkTitle.Text = "Add Parking";
            btnDeletePark.Visible = false;

            txtBankAccountNumber.Text = txtName.Text = hfId.Value = String.Empty;
        }

        protected void LoadParkingZones(int idPark)
        {
            ParkingManager.BindParkingZones(ref grdParkingZones, idPark);
            hfParkZoneid.Value = idPark.ToString();
            upParking.Update();
        }

        protected void LoadHours()
        {
            ddlCloseHour.DataSource = Hour.AM0100.AllHours();
            ddlCloseHour.DataBind();

            ddlOpenHour.DataSource = Hour.AM0100.AllHours();
            ddlOpenHour.DataBind();
        }

        protected void grdParkingZones_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditZone")
            {
                ParkingZone zone = ParkingManager.GetParkingZone(Convert.ToInt32(e.CommandArgument));
                if (zone != null)
                {
                    hfZoneid.Value = e.CommandArgument.ToString();
                    hfParkZoneid.Value = zone.ParkingID.ToString();
                    txtManagerCommision.Text = zone.ManagerCommisionAmount.ToString();
                    //TODO: Add parking zone fares
                    //txtPriceHour.Text = zone.PriceHour.ToString();
                    //txtReserve.Text = zone.ReserveAmount.ToString();
                    txtWebCommision.Text = zone.WebCommisionAmount.ToString();
                    txtZoneName.Text = zone.Name;
                    ddlCloseHour.SelectedValue = zone.CloseHour.ToHour().ToStringHour();
                    ddlOpenHour.SelectedValue = zone.OpenHour.ToHour().ToStringHour();
                    cbxAllowHalfHour.Checked = zone.AllowHalfHour;
                    txtRegularSpaces.Text = zone.RegularSpaces.ToString();
                    txtSpacesForReserve.Text = zone.SpacesToReserve.ToString();

                    btnSubmitZone.Text = "Update";
                    lblZoneTitle.Text = "Modify Zone";
                    btnDeleteZone.Visible = true;
                    //mpeAddZone.Show();
                    upParkingZoneForm.Update();
                    OpenDialog("pnlZoneForm");
                }
            }
            else if (e.CommandName == "DeleteZone")
            {
                DeleteZone(Convert.ToInt32(e.CommandArgument));
                CleanParkZoneForm();
            }
        }

        protected void btnSubmitZone_Click(object sender, EventArgs e)
        {
            //TODO: parking Zone Fares
            ParkingManager.SaveParkingZone(
                Convert.ToInt32(hfParkZoneid.Value),
                hfZoneid.Value,
                txtZoneName.Text,
                Convert.ToDecimal(txtManagerCommision.Text),
                Convert.ToDecimal(txtWebCommision.Text),
                cbxAllowHalfHour.Checked,
                ddlOpenHour.SelectedValue.ToHour().ToString(),
                ddlCloseHour.SelectedValue.ToHour().ToString(),
                Convert.ToInt32(txtRegularSpaces.Text),
                Convert.ToInt32(txtSpacesForReserve.Text),
                Profile.ManagerID,
                Request.UserHostAddress);
            CloseDialog("pnlZoneForm");
            CleanParkZoneForm();
            LoadParkingZones(Convert.ToInt32(hfParkZoneid.Value));
        }

        private void CleanParkZoneForm()
        {
            btnSubmitZone.Text = "Save";
            lblZoneTitle.Text = "Add Zone";
            btnDeleteZone.Visible = false;

            ddlCloseHour.SelectedIndex = ddlOpenHour.SelectedIndex = 1;
            txtManagerCommision.Text = txtZoneName.Text = txtWebCommision.Text = txtReserve.Text =
                txtPriceHour.Text = hfZoneid.Value = txtSpacesForReserve.Text = txtRegularSpaces.Text = String.Empty;
        }

        protected void btnCancelZone_Click(object sender, EventArgs e)
        {
            CleanParkZoneForm();
            CloseDialog("pnlZoneForm");
        }

        protected void btnDeleteZone_Click(object sender, EventArgs e)
        {
            DeleteZone(Convert.ToInt32(hfZoneid.Value));
            CleanParkZoneForm();
            upParking.Update();
            CloseDialog("pnlZoneForm");
        }

        private void DeleteZone(int id)
        {
            ParkingManager.DeleteParkingZone(id, Profile.ManagerID, Request.UserHostAddress);
            LoadParkingZones(Convert.ToInt32(hfParkZoneid.Value));
        }
    }
}
